/*
 * InputMenuGameState.java
 * 
 * Created on Jul 13, 2007, 8:43:26 PM
 * 
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.gamestate;

import com.jme.system.GameSettings;
import com.jme.util.GameTaskQueueManager;
import com.jmex.awt.swingui.JMEDesktopState;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import kanjitori.KanjiTori;
import kanjitori.preferences.KeyMap;
import kanjitori.preferences.KeyMapComboBoxModel;
import kanjitori.util.JMEDesktopUtil;
import kanjitori.util.VideoConstants;

import static kanjitori.util.Bundle.*;

/**
 *
 * @author DGronau
 */
public class InputMenuGameState  extends JMEDesktopState {

    private GameSettings gameSettings;
    private JCheckBox invertMouseBox;
    
    public InputMenuGameState(GameSettings settings) {
        this.gameSettings = gameSettings;
        GameTaskQueueManager.getManager().update(new Callable<Object>() {

            public Object call() throws Exception {
                initGraphics();
                return null;
            }
        });
    }
    
    private void initGraphics() {
        JDesktopPane jDesktop = getDesktop().getJDesktop();
        JMEDesktopUtil.init(jDesktop);
        
        Font font = new JLabel().getFont().deriveFont(Font.BOLD, 16f);

        for (int i = 0; i < KeyMap.ACTIONS.length; i++) {
           JMEDesktopUtil.setJLabel(jDesktop, getString("menu_" + KeyMap.ACTIONS[i]),
                   new Point(-10, -170 + i*30), font);
           JMEDesktopUtil.setJComboBox(jDesktop, new JComboBox(
                   new KeyMapComboBoxModel(KeyMap.ACTIONS[i],0)), new Point(10, -170 + i*30));
           JMEDesktopUtil.setJComboBox(jDesktop, new JComboBox(
                   new KeyMapComboBoxModel(KeyMap.ACTIONS[i],1)), new Point(120, -170 + i*30));
        }
        JMEDesktopUtil.setJLabel(jDesktop, getString("menu_invertMouse"), new Point(-10, 130), font);
        invertMouseBox = new JCheckBox();
        invertMouseBox.setSelected(KeyMap.isMouseInverted());
        jDesktop.add(invertMouseBox);
        JMEDesktopUtil.setSize(invertMouseBox);
        JMEDesktopUtil.pos(invertMouseBox, 10, 130, JMEDesktopUtil.H.LEFT, JMEDesktopUtil.V.CENTER);
        
        JButton okButton = new JButton(getString("menu_ok"));
        jDesktop.add(okButton);
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                save();
                InputMenuGameState.this.setActive(false);
                KanjiTori.setMenuGameState();
            }
        });

        JButton cancelButton = new JButton(getString("menu_cancel"));
        jDesktop.add(cancelButton);
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                InputMenuGameState.this.setActive(false);
                KanjiTori.setMenuGameState();
            }
        });

        JMEDesktopUtil.maxSize(okButton, cancelButton);
        JMEDesktopUtil.pos(okButton, -10, 170, JMEDesktopUtil.H.RIGHT, JMEDesktopUtil.V.CENTER);
        JMEDesktopUtil.pos(cancelButton, 10, 170, JMEDesktopUtil.H.LEFT, JMEDesktopUtil.V.CENTER);
    }
    
    private void save() {
        KeyMapComboBoxModel.save();   
        KeyMap.setMouseInverted(invertMouseBox.isSelected());
        KanjiTori.savePreferences();
    }
    
    public void reInit() {
        KeyMapComboBoxModel.reInit();
        invertMouseBox.setSelected(KeyMap.isMouseInverted());
    }
}
